package org.gwt.pluginframework.plugin;

import org.gwt.pluginframework.common.Plugin;
import org.gwt.pluginframework.common.PluginState;
import org.gwt.pluginframework.common.event.CommonEvents;
import org.gwt.pluginframework.common.event.IPluginEvent;
import org.gwt.pluginframework.common.event.PluginEventHandler;

import com.extjs.gxt.ui.client.event.FxEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.Window;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.impl.SchedulerImpl;
import com.google.gwt.user.client.ui.Label;
/*
@PluginManifest(name="SplashScreen", version="1.0")
@EventHandled(
	names={
		CommonEvents.PLUGIN_MANAGER_LOADED_EVENT, 
		CommonEvents.PLUGIN_STARTED_EVENT, 
		CommonEvents.PERSPECTIVE_LOADED_EVENT},
	handlers={
		"handlePluginManagerLoadedEvent",
		"handlePluginStartedEvent",
		"handlePerspectiveLoadedEvent"
})
*/
public class SplashScreen extends Plugin {

	private static final long serialVersionUID = -459964973706084910L;

	long start;
	
	protected Window window;
	
	public SplashScreen() {
		createUI();
	}
	
	protected void createUI() {
		start = System.currentTimeMillis();
		window = new Window();
		//window.setModal(true);
		window.setResizable(false);
		window.setClosable(false);
		window.setWidth(300);
		window.setHeight(300);
		window.setHeading("Splash");
		// splash.setHeaderVisible(false);

		Label title = new Label("Plugin Desktop Application");
		title.setStylePrimaryName("title");
		window.add(title);
		Label version = new Label("version 0.1");
		version.setStylePrimaryName("version");
		window.add(version);
		
		window.show();
	}
	
	@Override
	public void onInitialize() {
		registerEventHandler(new PluginEventHandler(CommonEvents.PLUGIN_MANAGER_INITIALIZED_EVENT) {
			public void handle(IPluginEvent event) {
				addMessage("Plugin manager initialized");
			} 
		});
		registerEventHandler(new PluginEventHandler(CommonEvents.PLUGIN_MANAGER_STARTED_EVENT) {
			public void handle(IPluginEvent event) {
				addMessage("Started in " + (System.currentTimeMillis() - start) + " ms");
				
				Scheduler scheduler = new SchedulerImpl();
				scheduler.scheduleDeferred(new Scheduler.ScheduledCommand() {
					public void execute() {
						FxConfig conf = new FxConfig(1500);
						conf.setEffectCompleteListener(new Listener<FxEvent>() {
							public void handleEvent(FxEvent be) {
								window.hide();
								//window = null;
								manifest.setState(PluginState.STOPED);
							}
						});
						window.el().fadeOut(conf);
					}
				});
			} 
		});
		registerEventHandler(new PluginEventHandler(CommonEvents.PLUGIN_INITIALIZED_EVENT) {
			public void handle(IPluginEvent event) {
				addMessage("Plugin '" + event.getInvoker().getName() + "' was initialized in " + event.getParameters().get("time") + "ms");
			} 
		});
		registerEventHandler(new PluginEventHandler(CommonEvents.PLUGIN_STARTED_EVENT) {
			public void handle(IPluginEvent event) {
				addMessage("Plugin '" + event.getInvoker().getName() + "' was started in " + event.getParameters().get("time") + "ms");
			} 
		});
		registerEventHandler(new PluginEventHandler(CustomEvents.PERSPECTIVE_INITIALIZED_EVENT) {
			public void handle(IPluginEvent event) {
				addMessage("Perspective '" + event.getInvoker().getName() + "' was initialized in " + event.getParameters().get("time") + "ms");
			} 
		});
	}
	
	protected void addMessage(String message) {
		window.add(new Label(message));
		window.layout();
	}

	// FIXME generate from annotation
//	private String[] events = new String[] {
//			CommonEvents.PLUGIN_MANAGER_LOADED_EVENT, 
//			CommonEvents.PLUGIN_MANAGER_STARTED_EVENT, 
//			CommonEvents.PLUGIN_LOADED_EVENT,
//			CommonEvents.PLUGIN_STARTED_EVENT,
//			CommonEvents.PERSPECTIVE_LOADED_EVENT};
	
	public void setupManifest() {		
		manifest.setName("SplashScreen");
		manifest.setVersion("1.0");
	}
}

